package codingforgreat.lchot150;

public class Lc041 {
    public int firstMissingPositive(int[] nums) {
        int L = 0;
        int R = nums.length;
        while(L < R){
            if(nums[L] == L + 1){
                L++;
            }else if(nums[L] <= 0 || nums[L] > R || nums[nums[L] - 1] == nums[L]){
                swap(nums,L,--R);
            }else{
                swap(nums,L,nums[L] - 1);
            }
        }
        return L + 1;

    }
    public  void swap(int[] arr, int i, int j) {
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }
}
